import SocketInstance from "../Tools/SocketInstance";
import { RequestCode, UserAction, loginData } from "../GlobalData/DataStruct";
import Tips from "../Tools/Tips";
import DataCenter from "../GlobalData/DataCenter";

export default class login_scene extends Laya.Script {
    private login_panel:Laya.Sprite
    private account_banner:Laya.Sprite
    private password_banner:Laya.Sprite
    private account_input:Laya.TextInput
    private password_input:Laya.TextInput
    private login_btn:Laya.Sprite
    private login_register_btn:Laya.Sprite

    private register_panel:Laya.Sprite
    private res_name_banner:Laya.Sprite
    private res_account_banner:Laya.Sprite
    private res_password_banner:Laya.Sprite
    private res_confirm_banner:Laya.Sprite
    private res_name_input:Laya.TextInput
    private res_account_input:Laya.TextInput
    private res_password_input:Laya.TextInput
    private res_confirm_input:Laya.TextInput
    private res_register_btn:Laya.Sprite
    private res_back_btn:Laya.Sprite
    
    constructor() { super(); }
    
    onEnable(): void 
    {
        this.initParams()
        this.initEvent()
    }
    initParams()
    {
        this.login_panel=this.owner.getChildByName("login_panel") as Laya.Sprite;
        this.account_banner=this.login_panel.getChildByName("account_banner") as Laya.Sprite
        this.password_banner=this.login_panel.getChildByName("password_banner") as Laya.Sprite
        this.account_input=this.account_banner.getChildByName("account_input") as Laya.TextInput
        this.password_banner=this.login_panel.getChildByName("password_banner") as Laya.TextInput
        this.password_input=this.password_banner.getChildByName("password_input") as Laya.TextInput
        this.login_btn=this.login_panel.getChildByName("login_btn") as Laya.Sprite
        this.register_panel=this.owner.getChildByName("register_panel") as Laya.Sprite
        this.res_name_banner=this.register_panel.getChildByName("name_banner") as Laya.Sprite
        this.res_account_banner=this.register_panel.getChildByName("account_banner") as Laya.Sprite
        this.res_password_banner=this.register_panel.getChildByName("password_banner") as Laya.Sprite
        this.res_confirm_banner=this.register_panel.getChildByName("confirm_banner") as Laya.Sprite
        this.res_name_input=this.res_name_banner.getChildByName("name_input") as Laya.TextInput
        this.res_account_input=this.res_account_banner.getChildByName("account_input") as Laya.TextInput
        this.res_password_input=this.res_password_banner.getChildByName("password_input") as Laya.TextInput
        this.res_confirm_input=this.res_confirm_banner.getChildByName("confirm_input") as Laya.TextInput
        this.res_register_btn=this.register_panel.getChildByName("register_btn") as Laya.Sprite
        this.res_back_btn=this.register_panel.getChildByName("back_btn") as Laya.Sprite
        this.login_register_btn=this.login_panel.getChildByName("register_btn") as Laya.Sprite
    }
    initEvent()
    {
        this.login_btn.on(Laya.Event.CLICK,this,this.onLoginBtn)
        this.res_register_btn.on(Laya.Event.CLICK,this,this.on_res_register)
        
        this.login_register_btn.on(Laya.Event.CLICK,this,this.switch_panel)
        this.res_back_btn.on(Laya.Event.CLICK,this,this.switch_panel)
        //监听回调消息
        Laya.stage.on("login",this,this.login_callback)
        Laya.stage.on("register",this,this.register_callback)
    }

    /**切换面板 */
    switch_panel()
    {
        if(this.login_panel.visible)
        {
            this.login_panel.visible=false;
        }
        else
        {
            this.login_panel.visible=true
        }
        if(this.register_panel.visible)
        {
            this.register_panel.visible=false;
        }
        else
        {
            this.register_panel.visible=true
        }
    }

    /**注册面板注册按钮函数 */
    on_res_register()
    {
        //获取用户名、账户、密码、确认密码
        let name_str=this.res_name_input.text
        let account_str=this.res_account_input.text
        let password_str=this.res_password_input.text
        let confirm_str=this.res_confirm_input.text

        if(name_str.length==0)
        {
            Tips.instance.showTips("用户名不能为空",this.owner)
            return
        }
        if(name_str.length>8)
        {
            Tips.instance.showTips("用户名不能大于8位",this.owner)
            return
        }
        if(this.checkInput(name_str))
        {
            Tips.instance.showTips("用户名不合法",this.owner)
            return
        }
        if(account_str.length<=0||account_str.length>10)
        {
            Tips.instance.showTips("账号不能为空或大于10位",this.owner)
            return
        }
        if(password_str.length<6)
        {
            Tips.instance.showTips("密码不能小于六位数",this.owner)
            return
        }
        if(confirm_str!=password_str)
        {
            Tips.instance.showTips("两次密码不一致",this.owner)
            return
        }
        
        //构建数据对象
        let res_data=new loginData()
        res_data.account=account_str
        res_data.password=password_str
        res_data.name=name_str

        //发送注册请求
        SocketInstance.instance.sendRequest(
            RequestCode.USER,
            UserAction.SIGNUP,
            res_data
        )
    }

    /**注册消息回调 */
    register_callback(data:any)
    {
        if(data.code==200)
        {
            if(data.data)
            {
                this.switch_panel()
                //将注册成功信息填入登录输入框内
                this.account_input.text=this.res_account_input.text
                this.password_input.text=this.res_password_input.text
            }
            Tips.instance.showTips(data.msg,this.owner)
        }
        else
        {
            Tips.instance.showTips("服务器异常,请联系管理员",this.owner)
        }
        
    }

    /**检查输入合法性 */
    checkInput(str:string)
    {
        console.log(str)
        for(let i=0;i<DataCenter.illegal_str.length;i++)
        {
            if(str.search(DataCenter.illegal_str[i])!=-1)
            {
                return true
            }
        }
    }

    /**登录回调函数 */
    login_callback(data:any)
    {
        if(data.code==200)
        {
            if(data.data)
            {
                Tips.instance.showTips(data.msg,this.owner)
                //设置全局用户名
                DataCenter.user_name=data.data.name
                //设置全局登录状态
                DataCenter.isLogin=true
                //切换到主场景
                Laya.Scene.open("main_scene.scene")
            }
            else
            {
                Tips.instance.showTips(data.msg,this.owner)
            }
        }
        
    }
    onLoginBtn()
    {
        //1.获取账号和密码文本
        let account_str=this.account_input.text
        let password_str=this.password_input.text
        //创建login_data对象
        let login_data=new loginData()
        login_data.account=account_str
        login_data.password=password_str
        //2.发送请求消息
        SocketInstance.instance.sendRequest(
            RequestCode.USER,
            UserAction.LOGIN,
            login_data
        )
    }
    onDisable(): void {
    }
}